package trucaso;

/*
 * The panel containing the Cards and background
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class CardZone extends JPanel implements MouseListener, MouseMotionListener {
	protected BufferedImage background,titleScreen;
	protected Deck deck;
	protected Dimension paneSize,size;
	protected Dimension[] dm;
	protected int pcp,ccp;
	protected Game game;
	protected int[] cardOrder = {0,1,2,3,4,5};
	
	protected CardZone(Dimension d,Deck de,Game ga) {
		super();
		setSize(d);
		paneSize = d;
		setBackground(new Color(35,134,38));
		deck = de;
		game = ga;
		
		setBackground("backgrounds/mcgallop.jpg");
		
		try {
			java.net.URL url = Deck.class.getResource("backgrounds/title.png");
			titleScreen = ImageIO.read(url);
		}catch(IOException ex) {
			titleScreen = null;
		}
		
		dm = new Dimension[6];
	
		dm[1] = new Dimension();
		dm[2] = new Dimension();
		dm[3] = new Dimension();
		dm[4] = new Dimension();
		dm[5] = new Dimension();
		dm[0] = new Dimension();
		
		size = new Dimension();
		
		pcp = 0;
		ccp = 0;
		
		addMouseListener(this);
		addMouseMotionListener(this);
		
	}
	
    public void mousePressed(MouseEvent e) {}
    public void mouseDragged(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    
    
    // Events to handle mouse clicks and play cards.
    public void mouseClicked(MouseEvent e) {
    	int x = e.getX();
    	int y = e.getY();
    	
    	if((y<paneSize.height-paneSize.height/20)&&(y>paneSize.height-paneSize.height/20-(int)(paneSize.height/2.7))) {
    		if((x>paneSize.width/25)&&(x<paneSize.width/25+paneSize.width/5)) {
    			if(game.getBothHands()[3].isInGame()) {
    				playCard(3,true);
    			}
    			
    		}
    	}
    	if((y<paneSize.height-paneSize.height/20)&&(y>paneSize.height-paneSize.height/20-(int)(paneSize.height/2.7))) {
    		if((x>(paneSize.width/25)*2+paneSize.width/5)&&(x<(paneSize.width/25)*2+(paneSize.width/5)*2)) {
    			if(game.getBothHands()[4].isInGame()) {
    				playCard(4,true);
    			}
    			
    		}
    	}
    	if((y<paneSize.height-paneSize.height/20)&&(y>paneSize.height-paneSize.height/20-(int)(paneSize.height/2.7))) {
    		if((x>(paneSize.width/25)*3+(paneSize.width/5)*2)&&(x<(paneSize.width/25)*3+(paneSize.width/5)*3)) {
    			if(game.getBothHands()[5].isInGame()) {
    				playCard(5,true);
    			}
    			
    		}
    	}
    }
    
    
	// Used to set a new panel size
	protected void newSize(Dimension d) {
		
		setSize(d);
		paneSize = d;
		if(game!=null) {
			setCards();
		}
		//New card Dimensions
		size = new Dimension(paneSize.width/5,(int)(paneSize.height/2.7));
		repaint();
		
	}
	
	protected synchronized void reset() {
		ccp = 0;
		pcp = 0;
		cardOrder = new int[] {0,1,2,3,4,5};
		setCards();
	}
	
	// Method to play the cards
	// 0,1,2 are opponent's cards, 3,4,5 are the player's cards
	protected synchronized void playCard(int cardNumber,boolean visible) {
		int aux;
		if(cardNumber<3) {
			
			dm[cardNumber].width = (paneSize.width/25)*4+(paneSize.width/5)*3;
			dm[cardNumber].height = (paneSize.height/20) + ccp*paneSize.height/20;
			ccp++;
			
			if((ccp==1)&&(cardNumber!=cardOrder[0])) {
				aux = cardOrder[0];
				cardOrder[0] = cardNumber;
				if(cardOrder[1]!=cardNumber) {
					cardOrder[2] = aux;
				} else {
					cardOrder[1] = aux;
				}
				
			} else {
				if((ccp==2)&&(cardNumber!=cardOrder[1])) {
					aux = cardOrder[1];
					cardOrder[1] = cardNumber;
					cardOrder[2] = aux;
				}
			}
	
			game.getBothHands()[cardNumber].setVisible(visible);
			game.getBothHands()[cardNumber].setInGame(false);
			
			
		} else {
			
			dm[cardNumber].width = (paneSize.width/25)*4+(paneSize.width/5)*3;
			dm[cardNumber].height = paneSize.height-paneSize.height/20-size.height-(paneSize.height/20)*pcp;
			if(visible) game.playCard(cardNumber-3);
			pcp++;
			if((pcp==1)&&(cardNumber!=cardOrder[3])) {
				game.actions.disableEnvido();
				aux = cardOrder[3];
				cardOrder[3] = cardNumber;
				if(cardOrder[4]!=cardNumber) {
					cardOrder[5] = aux;
				} else {
					cardOrder[4] = aux;
				}
				
			} else {
				if((pcp==2)&&(cardNumber!=cardOrder[4])) {
					aux = cardOrder[4];
					cardOrder[4] = cardNumber;
					cardOrder[5] = aux;
				}
			}
			game.getBothHands()[cardNumber].setVisible(visible);
			game.getBothHands()[cardNumber].setInGame(false);
			
		}
		
		repaint();
	}
	
	// Method to change the background
	protected void setBackground(String file) {
		try {
			java.net.URL url = Deck.class.getResource(file);
			background = ImageIO.read(url);
		}catch(IOException ex) {
			background = null;
		}catch(IllegalArgumentException ex) {
			try {
				background = ImageIO.read(new File(file));
			}catch(IOException ioex) {
				background = null;
			
				JOptionPane.showMessageDialog(this,"An error ocurred while loading \n"+file+"\nand the " +
				"background got screwed","So Long Background!",JOptionPane.ERROR_MESSAGE);
			}
		}
		repaint();
	}
	
	
	// Places the cards in the initial positions
	protected synchronized void setCards() {
			dm[0].width = paneSize.width/25;
			dm[0].height = paneSize.height/20;
			dm[1].width = dm[0].width*2+paneSize.width/5;
			dm[1].height = dm[0].height;
			dm[2].width = dm[0].width*3+(paneSize.width/5)*2;
			dm[2].height = dm[0].height;
		
			dm[3].width = dm[0].width;
			dm[3].height = paneSize.height-paneSize.height/20-(int)(paneSize.height/2.7);
			dm[4].width = dm[1].width;
			dm[4].height = dm[3].height;
			dm[5].width = dm[2].width;
			dm[5].height = dm[3].height;
			
			boolean started = false;
			pcp = 0;
			ccp = 0;
			for(int i=0;i<6;i++) {
				if(!game.getBothHands()[cardOrder[i]].isInGame()) {
					started = true;
				}
			}
			if(started) {
				for(int i=0;i<6;i++) {
					if(!game.getBothHands()[cardOrder[i]].isInGame()) {
						playCard(cardOrder[i],true);
					}
				}
			}else{			
				cardOrder = new int[]{0,1,2,3,4,5};
			}
			
	}
	
	protected int[] getCardOrder() {
		return cardOrder;
	}
	
	//Pretty obvious...
	protected synchronized void drawCard(Graphics2D g,Card c,Dimension xy,Dimension s) {
		if(c.isVisible()) {
			g.drawImage(c.getCard(),xy.width,xy.height,s.width,s.height,null);
		} else {
			g.drawImage(Card.getCardBack(),xy.width,xy.height,s.width,s.height,null);
		}
	}
	
	protected synchronized void drawCards(Graphics2D g,Card[] c) {
		if(c==null) return;
		if(c[0]==null) return;
		int l = c.length;
		for(int i = 0;i<l;i++) {
			drawCard(g,c[cardOrder[i]],dm[cardOrder[i]],size);
		}
	}
	
		
	protected void drawBackground(Graphics2D g) {
		if(background==null) return;
		g.drawImage(background,0,0,paneSize.width,paneSize.height,null);
	}
	
	protected void drawTitleScreen(Graphics2D g) {
		if(titleScreen==null) return;
		g.drawImage(titleScreen,0,0,paneSize.width,paneSize.height,null);
	}
		

	protected void paintComponent(Graphics arg0) {
		super.paintComponent(arg0);
		Graphics2D g2d = (Graphics2D)arg0;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		if(game!=null) {
			drawBackground(g2d);
			drawCards(g2d,game.getBothHands());
		}else{
			drawTitleScreen(g2d);
		}
	}
	
}
